﻿using System.Data.SqlClient;
using System;
using System.Data;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
namespace YZZ
{
    class Tools
    {
        //public static double MeasureTextWidth(String str, double fontsize, string fontName, IntPtr m_hWnd)
        //{
        //    double width = -1;
        //    try
        //    {
        //        System.Drawing.Graphics graphics = System.Drawing.Graphics.FromHwnd(m_hWnd);
        //        graphics.PageUnit = System.Drawing.GraphicsUnit.Point;
        //        System.Drawing.Font font = new System.Drawing.Font(fontName, (float)fontsize);


        //        System.Drawing.SizeF size = graphics.MeasureString(str, font);

        //        width = size.Width;

        //        font.Dispose();
        //        graphics.Dispose();
        //    }
        //    catch (System.Exception e)
        //    {
        //    }
        //    return width;
        //}
        internal static string ShouldHaveNum(string strNum, string UseThisIfNoNum)
        {
            double num = StringToDouble(strNum);
            if (num > 0)
                return num.ToString();
            return UseThisIfNoNum;
        }
        internal static string ShouldHaveString(string str, string UseThisIfNoNum)
        {
            if (!string.IsNullOrEmpty(str) && str.ToLower() != "null")
                return "'" + str + "'";
            return UseThisIfNoNum;
        }
        public static string Filt(string executed, string filter)
        {
            string result = string.Empty;
            foreach (char c in executed.ToCharArray())
            {
                foreach (char f in filter.ToCharArray())
                {
                    if (c == f) result += c;
                }
            }
            return result;
        }
        public static string FillBits(int code, int targetbit)
        {
            string result = string.Empty;
            int limit = (int)System.Math.Pow(10.0f, (double)targetbit);
            if (code >= limit) return result; // 超出范围报错
            for (int i = targetbit; i > 0; i--)
            {
                int innerlimit = (int)System.Math.Pow(10.0f, (double)(i - 1));
                if (code >= innerlimit)
                {
                    result += code.ToString();
                    return result;
                }
                else
                    result += "0";
            }
            return result;
        }
        public static string ExcludeString(string Msg, string ToExclude, bool toLower = true)
        {
            if (toLower)
            {
                Msg = Msg.ToLower();
                ToExclude = ToExclude.ToLower();
            }
            string result = string.Empty;
            for (int i = 0; i < Msg.Length; i++)
            {
                int length = 0;
                if (i + ToExclude.Length < Msg.Length)
                    length = ToExclude.Length;
                else
                    length = Msg.Length - i;
                if (Msg.Substring(i, length) != ToExclude)
                    result += Msg.Substring(i, 1);
                else
                {
                    for (int j = 1; j < ToExclude.Length; j++)
                    {
                        i++;
                    }
                    continue;
                }
            }
            return result;
        }
        public static string ReplaceString(string Msg, string ToBeReplaced,string ToReplace, bool toLower = true)
        {
            if (toLower)
            {
                Msg = Msg.ToLower();
                ToBeReplaced = ToBeReplaced.ToLower();
            }
            string result = string.Empty;
            for (int i = 0; i < Msg.Length; i++)
            {
                int length = 0;
                if (i + ToBeReplaced.Length < Msg.Length)
                    length = ToBeReplaced.Length;
                else
                    length = Msg.Length - i;
                if (Msg.Substring(i, length) != ToBeReplaced)
                    result += Msg.Substring(i, 1);
                else
                {
                    for (int j = 1; j < ToBeReplaced.Length; j++)
                    {
                        i++;
                    }
                    result += ToReplace;
                    continue;
                }
            }
            return result;
        }
        //protected bool HasString(string In, string String, bool toLower = true)
        //{
        //    if (toLower)
        //    {
        //        In = In.ToLower(); String = String.ToLower();
        //    }
        //    string resultstring = string.Empty;
        //    for (int i = 0; i < In.Length; i++)
        //    {
        //        int length = 0;
        //        if (i + String.Length < In.Length)
        //            length = String.Length;
        //        else
        //            length = In.Length - i;
        //        if (In.Substring(i, length) != String)
        //            resultstring += In.Substring(i, 1);
        //        else
        //        {
        //            return true;
        //        }
        //    }
        //    return false;
        //}

        public static string FillupIndexs(string newIndex, int bits)
        {
            string result = newIndex;
            if (result.Length > bits)
                result = (Math.Pow(10, bits) - 1).ToString();
            else
            {
                int length = result.Length;
                for (int i = 0; i < bits - length; i++)
                {
                    result = "0" + result;
                }
            }
            return result;
        }
        public static string FillupIndexs(double newIndex, int bits)
        {
            string result = newIndex.ToString();
            if (result.Length > bits)
                result = (Math.Pow(10, bits) - 1).ToString();
            else
            {
                int length = result.Length;
                for (int i = 0; i < bits - length; i++)
                {
                    result = "0" + result;
                }
            }
            return result;
        }
        public static double MeasureTextWidth(string text, double fontSize, string fontFamily/*, double pixelsPerDip*/)
        {

            FormattedText formattedText = new FormattedText(
            text,
            System.Globalization.CultureInfo.InvariantCulture,
            FlowDirection.LeftToRight,
            new Typeface(fontFamily),
            fontSize,
            Brushes.Black/*,pixelsPerDip*/
            );
            return formattedText.WidthIncludingTrailingWhitespace;
        }
        public static string AddQuote(string msg, string toquote)
        {
            string result = string.Empty;
            result = toquote + msg + toquote;
            return result;
        }
        public static bool HasString(string In, string String, bool toLower = true)
        {
            if (toLower)
            {
                In = In.ToLower(); String = String.ToLower();
            }
            string resultstring = string.Empty;
            for (int i = 0; i < In.Length; i++)
            {
                int length = 0;
                if (i + String.Length < In.Length)
                    length = String.Length;
                else
                    length = In.Length - i;
                if (In.Substring(i, length) != String)
                    resultstring += In.Substring(i, 1);
                else
                {
                    return true;
                }
            }
            return false;
        }
        public static string AddQuote(string msg, string front, string end)
        {
            return front + msg + end;
        }

        internal static int StringToInt(string s)
        {
            return (int)StringToDouble(s);
        }

        internal static double StringToDouble(string s)
        {
            double i = 0;
            if (string.IsNullOrEmpty(s)) return i;
            if (s.Trim().ToLower() == "true")
                return 1.0f;
            string target = string.Empty;
            string nums = "0123456789.-";
            foreach (char c in s.ToCharArray())
            {
                foreach (char nc in nums.ToCharArray())
                {
                    if (c == nc)
                        target += c;
                }
            }
            double.TryParse(target, out i);
            return i;
        }
    }

    static public class StaticMemory
    {
        //static private Dictionary<string, bool> ControlStatus = new Dictionary<string, bool>();
        #region 类内变量
        static private string CurrentControlName { get; set; }
        static private List<string> SpecialFlag = new List<string>();
        static private List<KeyValuePair<string, int>> TempData { get; set; }
        static bool inited = false;
        #endregion
        #region 操作方法
        static public bool IsStillInControl(string ControlName)
        {
            if (CurrentControlName == ControlName)
                return true;
            return false;
        }
        static public void SetControll(string ControlName)
        {
            if (CurrentControlName != ControlName)
            {
                SpecialFlag.Clear();
            }
            CurrentControlName = ControlName;

        }
        static internal bool HasSpecialFlag(string v)
        {
            if (SpecialFlag.IndexOf(v) < 0)
                return false;
            return true;
        }
        static internal void SetSpecialFlag(string v)
        {
            SpecialFlag.Add(v);
        }
        static private void init()
        {
             TempData = new List<KeyValuePair<string, int>>();
            inited = true;
    }
        static public void SetValue(string key, int value)
        {
            if (!inited)
                init();
            KeyValuePair<string, int> item = new KeyValuePair<string, int>(key, value);
            int index = GetTempDataIndex(key);
            if (index >= 0)
                TempData.RemoveAt(index);
            TempData.Add(item);
        }
        static public void AddValue(string key, int value)
        {
            if (!inited)
                init();
            int index = GetTempDataIndex(key);
            int currentValue = 0;
            if (index >= 0)
                currentValue = TempData[index].Value;
            value += currentValue;
            SetValue(key, value);
        }
        static public int GetValue(string key)
        {
            if (!inited)
                init();
            int index = GetTempDataIndex(key);
            if (index >= 0)
                return TempData[index].Value;
            return -1;
        }
        static private int GetTempDataIndex(string key)
        {
            if (!inited)
                init();
            int index = 0;
            foreach (KeyValuePair<string, int> i in TempData)
            {
                if (i.Key == key)
                    return index;
                index++;
            }
            return -1;
        }
        #endregion

    }
}